<template>
  <div class="components-container">
    <div class="header-form">
      <div class="layui-inline">
        <label>火化日期从</label>
        <div class="layui-input-inline">
          <el-date-picker v-date v-model="selectData.CremationdateGreaterAndEquals" type="date" placeholder="请选择日期"
            style="width:160px;" /> 到
          <el-date-picker v-date v-model="selectData.CremationdateLessAndEquals" type="date" placeholder="请选择日期"
            style="width:160px;" />
        </div>
      </div>
      <div class="layui-inline" style="margin-left:20px" v-if="isBySar">
        <label>代办地址</label>
        <div class="layui-input-inline">
          <el-cascader style="width:305px" class="dz" ref="address" v-model="arr" :options="addressOptions"
            change-on-select clearable>
          </el-cascader>
        </div>
      </div>
      <el-button type="primary" @click="btnSearch" :loading="this.$store.state.isloading" style="margin-left:20px;">搜索
      </el-button>
      <el-button @click="btnRepeat">重置</el-button>
    </div>

    <!-- 功能列表 -->
    <div style="margin-top:10px;">
      <ul class="toolList">
        <li>
          <el-button type="primary" @click="btnSearch" :loading="this.$store.state.isloading">刷新</el-button>
        </li>
        <!-- <li><el-button type="primary" @click="exportExcel">导出Excel</el-button></li> -->
        <li v-if="btnShow(item.BTNVALUE)" v-for="(item, index) in $store.state.btnListCommon"
          @click="operate(item.BTNVALUE)">
          <el-button type="primary" :value="item.BTNSHOWNAME" v-text="item.BTNSHOWNAME"></el-button>
        </li>
      </ul>
    </div>
    <!-- 数据列表 -->
    <dataTable :headList="headList" :dataList="dataList" :hiddenColumn="false" :hiddenPageOrder="false" :isIndex='true'
      :pageOrder="pageOrder" @pageChange="pageChange" @selectRow="selectRow"></dataTable>
    <printmodal ref="printPdf"></printmodal>
  </div>
</template>
<style rel="stylesheet/scss" lang="scss" scoped>
  @import "src/styles/common.scss";

  .el-button+.el-button {
    margin-left: 0;
  }

  .btnCenter {
    text-align: center;
  }

</style>
<script>
  import sweetModal from "@/components/sweetModal";
  import dataTable from "@/components/Table/DataTable";
  import printmodal from "@/components/Pdf/Print";
  import apiBase from "@/api/apiBase.js";
  import {
    formatDate,
    timeStringToDate
  } from "@/utils/date.js";
  import {
    manage
  } from "@/mixins/managePage.js";
  export default {
    name: "assUse",
    mixins: [manage],
    data() {
      return {
        pageWidth: 1200,
        selectData: {
          CremationdateGreaterAndEquals: null,
          CremationdateLessAndEquals: null
        },
        printUrl: null,
        title: null,
        //代办地址
        addressOptions: [],
        arr: [],
        isUseNewAddress:null,
        isBySar: null,
      };
    },
    methods: {
      exportExcel() {
        var header
        var startTime
        if (this.selectData.CremationdateGreaterAndEquals) {
          startTime = this.selectData.CremationdateGreaterAndEquals.toLocaleDateString();
        } else {
          startTime = '空';
        }

        if (this.selectData.CremationdateLessAndEquals != null) {
          var endTime = this.selectData.CremationdateLessAndEquals.toLocaleDateString();
          header = localStorage.getItem('orgname') + '联办事项完成情况统计' + '(' + startTime + '--' + endTime + ')';
        } else {
          header = localStorage.getItem('orgname') + '联办事项完成情况统计' + '(' + startTime + '--' + '空' + ')';
        }
        if (this.dataList.length === 0) {
          this.$message.error("无导出数据！");
          return;
        }
        this.downloadLoading = true;

        require.ensure([], () => {
          const {
            export_json_to_excel
          } = require("@/vendor/Export2Excel");
          export_json_to_excel(this.headList, this.dataList, header);
          this.downloadLoading = false;
        });
      },
      //  功能操作
      operate(order) {
        this.order = order;
        switch (this.order) {
          //   case '导出Excel':
          case '导出':
            this.exportExcel()
            break;
          case '打印':
            this.$refs.printPdf.report(
              '打印联办事项完成情况统计',
              this.getDataC()
            );
            break;
        }
      },

      //获取表头
      getCreColumn() {
        let data = {};
        apiBase.DoService(
          data,
          apiBase.getApiAdress().Column.GetCompleteRateColumns,
          res => {
            this.headList = res.data.content;
          });
      },
      getaddress() {
        // 获取代办地址级联选择
        apiBase.DoService({}, apiBase.getApiAdress().Code.GetBoroughByCode, res => {
          let data = res.data.content;
          this.addressOptions = data.map(item => ({
            label: item.SARNAME,
            value: item.SARID,
            level: item.SARLEVEL,
            children: item.Children.map(item => ({
              label: item.SARNAME,
              value: item.SARID,
              level: item.SARLEVEL,
              children: this.restructur(item.Children)
            }))
          }));
        });
      },
      restructur(value) {
        let arr = JSON.parse(JSON.stringify(value));
        var data = [];
        arr.forEach(item => {
          if (item.Children.length > 0) {
            data.push({
              label: item.SARNAME,
              value: item.SARID,
              level: item.SARLEVEL,
              children: this.restructur(item.Children)
            });
          } else {
            data.push({
              label: item.SARNAME,
              value: item.SARID,
              level: item.SARLEVEL,
            });
          }
        });
        return data;
      },
      dealData(id,option){
        let level = ''
        for (let index = 0; index < option.length; index++) {
          if(id==option[index].value){
            return option[index].level
          }else if(option[index].children){
            if(this.dealData(id,option[index].children)){
              return this.dealData(id,option[index].children)
            }
          }
        }
      },
      //获取查询数据
      getData() {
        this.dataList = [];
        let Data = JSON.parse(JSON.stringify(this.selectData));
        if (Data.CremationdateGreaterAndEquals)
          Data.CremationdateGreaterAndEquals = formatDate(Data.CremationdateGreaterAndEquals, "yyyyMMdd");
        if (Data.CremationdateLessAndEquals)
          Data.CremationdateLessAndEquals = formatDate(Data.CremationdateLessAndEquals, "yyyyMMdd");
        Data.JpmSaridLike = this.arr[this.arr.length - 1];
        Data.SARID = this.arr[this.arr.length - 1];
        if(this.isUseNewAddress){
          Data.JpmSaridLike = null
        }else{
          if (Data.JpmSaridLike) {
            let level = this.dealData(Data.JpmSaridLike, this.addressOptions);
            switch (level) {
              case "3":
                Data.JpmSaridLike = Data.JpmSaridLike.substring(0, 6);
                break;
              case "4":
                Data.JpmSaridLike = Data.JpmSaridLike.substring(0, 9);
                break;
              case "5":
                Data.JpmSaridLike = Data.JpmSaridLike.substring(0, 12);
                break;
              default:
                break;
            }
          }
          Data.SARID = null
        }
        let data = {
          PageIndex: -1,
          PageSize: 0,
          OrderBy: this.pageOrder.orderType,
          OrderByType: this.pageOrder.orderSort,
          Data: Data
        };
        apiBase.DoService(
          data,
          apiBase.getApiAdress().Report.QueryCompleteRateStatistics,
          res => {
            this.dataList = res.data.content.OutData;
          });
      },
      getDataC() { //打印
        let Data = JSON.parse(JSON.stringify(this.selectData));
        if (Data.CremationdateGreaterAndEquals)
          Data.CremationdateGreaterAndEquals = formatDate(Data.CremationdateGreaterAndEquals, "yyyyMMdd");
        if (Data.CremationdateLessAndEquals)
          Data.CremationdateLessAndEquals = formatDate(Data.CremationdateLessAndEquals, "yyyyMMdd");
        Data.JpmSaridLike = this.arr[this.arr.length - 1];
        Data.SARID = this.arr[this.arr.length - 1];
        if(this.isUseNewAddress){
          Data.JpmSaridLike = null
        }else{
          if (Data.JpmSaridLike) {
            let level = this.dealData(Data.JpmSaridLike, this.addressOptions);
            switch (level) {
              case "3":
                Data.JpmSaridLike = Data.JpmSaridLike.substring(0, 6);
                break;
              case "4":
                Data.JpmSaridLike = Data.JpmSaridLike.substring(0, 9);
                break;
              case "5":
                Data.JpmSaridLike = Data.JpmSaridLike.substring(0, 12);
                break;
              default:
                break;
            }
          }
          Data.SARID = null
        }
        let data = {
          PageIndex: -1,
          PageSize: 100,
          OrderBy: this.pageOrder.orderType,
          OrderByType: this.pageOrder.orderSort,
          Data: Data
        };
        return data;
      },
      //重置查询
      btnRepeat() {
        this.selectData = {
          CremationdateGreaterAndEquals: null,
          CremationdateLessAndEquals: null,
        };
        this.arr = [];
        this.getData();
      },
      // 控制是否显示
      btnShow(val) {
        switch (val) {
          //   case '导出Excel':
          case '导出':
          case '打印':
            return true
            break;
        }
      },
    },
    components: {
      dataTable,
      sweetModal,
      printmodal
    },
    created() {
      //是否启用新的联办地点查询（舟山联办）
      apiBase.DoService({}, apiBase.getApiAdress().Param.GetIsUseNewAddress, res => {
        this.isUseNewAddress = res.data.content;
      });
      //控制地址下拉显隐
      apiBase.DoService({}, apiBase.getApiAdress().Param.GetIsBySar, res => {
        this.isBySar = res.data.content;
      });
      this.getaddress();
      this.getCreColumn();
      this.getData();
    }
  };

</script>
